﻿//+---------------------------------------------
//| js工具类
//| Create By:2009-4-20
//| Author:YuBing
//+--------------------------------------------- 
/* 
将String类型解析为Date类型. 
parseDate('2006-1-1') return new Date(2006,0,1) 
parseDate(' 2006-1-1 ') return new Date(2006,0,1) 
parseDate('2006-1-1 15:14:16') return new Date(2006,0,1,15,14,16) 
parseDate(' 2006-1-1 15:14:16 ') return new Date(2006,0,1,15,14,16); 
parseDate('2006-1-1 15:14:16.254') return new Date(2006,0,1,15,14,16,254) 
parseDate(' 2006-1-1 15:14:16.254 ') return new Date(2006,0,1,15,14,16,254) 
parseDate('不正确的格式') retrun null 
*/
function parseDate(str) {
	if (typeof str == 'string') {
		var results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) *$/);
		if (results && results.length > 3)
			return new Date(parseInt(results[1]), parseInt(results[2]) - 1, parseInt(results[3]));
		results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);
		if (results && results.length > 6)
			return new Date(parseInt(results[1]), parseInt(results[2]) - 1, parseInt(results[3]), parseInt(results[4]), parseInt(results[5]), parseInt(results[6]));
		results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9}) *$/);
		if (results && results.length > 7)
			return new Date(parseInt(results[1]), parseInt(results[2]) - 1, parseInt(results[3]), parseInt(results[4]), parseInt(results[5]), parseInt(results[6]), parseInt(results[7]));
	}
	return null;
}


//+---------------------------------------------
//| 将Date/String类型,解析为String类型. 
//| 传入String类型,则先解析为Date类型 
//| 不正确的Date,返回 '' 
//| 如果时间部分为0,则忽略,只返回日期部分. 
//+---------------------------------------------
function formatDate(v) {
	if (typeof v == 'string') v = parseDate(v);
	if (v instanceof Date) {
		var y = v.getFullYear();
		var m = v.getMonth() + 1;
		var d = v.getDate();
		var h = v.getHours();
		var i = v.getMinutes();
		var s = v.getSeconds();
		var ms = v.getMilliseconds();
		if (ms > 0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s + '.' + ms;
		if (h > 0 || i > 0 || s > 0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
		return y + '-' + m + '-' + d;
	}
	return '';
}



//+---------------------------------------------------  
//| 字符串转成日期类型   
//| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd  
//+---------------------------------------------------  
function stringToDate(DateStr) {
	if (DateStr == '' || DateStr == 'undefined') {
		return null;
	}
	var converted = Date.parse(DateStr);
	var myDate = new Date(converted);
	if (isNaN(myDate)) {
		//var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';  
		var arys = DateStr.split('-');
		myDate = new Date(arys[0], --arys[1], arys[2]);
	}
	return myDate;
}

//+------------------------------------------------------------
//| 获取一个随机数
//+------------------------------------------------------------
function getRandomNumber() {
	var Timer = new Date();
	var seconds = Timer.getSeconds();
	var randomnumber = Math.floor(Math.random() * 10000) + seconds;
	return randomnumber;
}

//+---------------------------------------------------  
//| 比较两个时间的大小   
//| time1 < time2 return -1;
//| time1 == time2 return 0;
//| time1 > time2 return 1;
//+--------------------------------------------------- 
function compareDate(time1, time2) {
	var i = time1 - time2;
	if (i < 0)
		return -1;
	if (i > 0)
		return 1;
	return 0;
}



//+---------------------------------------------------  
//| request对象
//+--------------------------------------------------- 
function request(strName) {
	var strHref = window.document.location.href;
	var intPos = strHref.indexOf("?");
	var strRight = strHref.substr(intPos + 1);

	var arrTmp = strRight.split("&");
	for (var i = 0; i < arrTmp.length; i++) {
		var arrTemp = arrTmp[i].split("=");

		if (arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
	}
	return "";
}



//+---------------------------------------------------  
//| string.Trim(),htmlEncode(),htmlDecode()
//+--------------------------------------------------- 
String.prototype.trim = function () {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.lTrim = function () {
	return this.replace(/(^\s*)/g, "");
}

String.prototype.rTrim = function () {
	return this.replace(/(\s*$)/g, "");
}

String.prototype.htmlEncode = function () {
	var s = "";
	if (this.length == 0) return "";
	s = this.replace(/&/g, "&amp;");
	s = s.replace(/</g, "&lt;");
	s = s.replace(/>/g, "&gt;");
	s = s.replace(/ /g, "&nbsp;");
	s = s.replace(/\'/g, "&#39;");
	s = s.replace(/\"/g, "&quot;");
	s = s.replace(/\r\n/g, "<br>");
	s = s.replace(',', "，");
	return s;
}
String.prototype.htmlDecode = function () {
	var s = "";
	if (this.length == 0) return "";
	s = this.replace(/&amp;/g, "&");
	s = s.replace(/&lt;/g, "<");
	s = s.replace(/&gt;/g, ">");
	s = s.replace(/&nbsp;/g, " ");
	s = s.replace(/&#39;/g, "\'");
	s = s.replace(/&quot;/g, "\"");
	s = s.replace(/<br>/g, "\r\n");
	s = s.replace(/&#183;/g, "·");
	s = s.replace("，", ",");
	return s;
}
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 




//+---------------------------------------------------  
//| Cookie函数
//+--------------------------------------------------- 
function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}

	if (start == -1) return null;
	var end = document.cookie.indexOf(';', len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value, expires, path, domain, secure) {

	var today = new Date();
	today.setTime(today.getTime());
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + '=' + escape(value) +
	((expires) ? ';expires=' + expires_date.toGMTString() : '') +
	((path) ? ';path=' + path : '') +
	((domain) ? ';domain=' + domain : '') +
	((secure) ? ';secure' : '');

}

function deleteCookie(name, path, domain) {

	if (getCookie(name)) document.cookie = name + '=' +
		((path) ? ';path=' + path : '') +
		((domain) ? ';domain=' + domain : '') +
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';

}
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 



//+---------------------------------------------------  
//| Array.strip()
//+--------------------------------------------------- 
Array.prototype.container = function (element) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element)
			return true;
	}
	return false;
}
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 

//+---------------------------------------------------  
//| Array.uniq() 清除Array中的重复项
//+--------------------------------------------------- 
Array.prototype.uniq = function () {
	var temp = {}, len = this.length;

	for (var i = 0; i < len; i++) {
		if (typeof temp[this[i]] == "undefined") {
			temp[this[i]] = 1;
		}
	}
	this.length = 0;
	len = 0;
	for (var i in temp) {
		this[len++] = i;
	}
	return this;
}
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 

//+---------------------------------------------------  
//| dom对象绑定事件，可带参数绑定，支持IE6+,FF1+
//+--------------------------------------------------- 
function Handler() { };
Handler.prototype = {
	/*
	* 把eventType类型的事件绑定到element元素，并使用handler事件句柄进行处理
	* 兼容 IE 及 Firefox 等浏览器
	*
	* @param element 在其上注册事件的对象（Object）
	* @param eventType 注册的事件类型（String），不加“on”
	* @param handler 事件句柄（Function）
	*/
	registerEvent: function (element, eventType, handler) {
		if (element.attachEvent) { //2级DOM的事件处理
			element.attachEvent('on' + eventType, handler);
		} else if (element.addEventListener) {
			element.addEventListener(eventType, handler, false);
		} else { //0级DOM的事件处理
			element['on' + eventType] = handler;
		}
	},
	/*
	* 获得带参数的事件句柄的引用
	*
	* @param obj 需要绑定事件处理函数的所有者，null 表示 window 对象
	* @param func 需要绑定的事件处理函数名
	* @param ... 第三个参数开始为绑定事件处理函数的参数，由 0 到多个构成
	*/
	bind: function (obj, handler) {
		obj = obj || window;
		var args = [];
		for (var i = 2; i < arguments.length; i++) {
			args.push(arguments[i]);
		}
		return function () { handler.apply(obj, args) };
	}
}
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 

//+---------------------------------------------------  
//| Javascript Array 的 indexOf() 方法 
//+--------------------------------------------------- 
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function (elt /*, from*/) {
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0)
			from += len;
		for (; from < len; from++) {
			if (from in this && this[from] === elt)
				return from;
		}
		return -1;
	};
}

function validatorForm() {
//	var reg1 = /^+?\d+(?:\.\d{0,2})?$/
//	var reg2 = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/
//	$("input[CheckedEmpty='true']").each(function () {
//		if ($(this).val() == "") {
//			showNoticMsg($(this)[0].id, $(this).attr("CheckedEmpty_Message"));
//			return false;
//		}
//		$(this).val($(this)[0].value.htmlEncode());
//	});
//	$("input[CheckedNumber='true']").each(function () {
//		if (/^+?\d+(?:\.\d{0,2})?$/.test($(this).val())) {
//			showNoticMsg($(this)[0].id, $(this).attr("CheckedNumber_Message"));
//			return false;
//		}
//	});
//	$("input[CheckedNumber='true']").each(function () {
//		if (!reg1.test($(this).val())) {
//			showNoticMsg($(this)[0].id, $(this).attr("CheckedNumber_Message"));
//			return false;
//		}
//	});
//	$("input[CheckedDate='true']").each(function () {
//		if (!reg2.test($(this).val())) {
//			showNoticMsg($(this)[0].id, $(this).attr("CheckedDate_Message"));
//			return false;
//		}
//	});

	return true;
}